
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.tompai.common.lock;

/**
* @desc: tompai-common
* @name: FileLockClient.java
* @author: tompai
* @email：liinux@qq.com
* @createTime: 2021年2月7日 下午11:42:37
* @history:
* @version: v1.0
*/

/**
 	* 实际上，使用文件锁是困难的；锁定机制是不可移植的。我们需要考虑到这一点来设计锁定逻辑。
	在POSIX系统中，锁是建议性的。读取或写入给定文件的不同进程必须就锁定协议达成一致。
	这将确保文件的完整性。操作系统本身不会强制任何锁定。
	在Windows上，除非允许共享，否则锁将是独占的。讨论操作系统特定机制的优点或缺点超出了本文的讨论范围。
	然而，在实现锁定机制时，了解这些细微差别很重要。
* @desc: tompai-common
* @name: FileLockClient.java
* @author: tompai
* @email：liinux@qq.com
* @createTime: 2021年2月8日 上午12:18:10
* @history:
* @version: v1.0
 */
public class FileLockClient {

	/**
	* @author: tompai
	* @createTime: 2021年2月7日 下午11:42:37
	* @history:
	* @param args void
	*/

	/*共享锁：获取到共享锁，会阻止获取独占锁，但不会阻止获取共享锁。获取到共享锁可以同时读，但只能有一个在写。
	独占锁：获取到独占锁后，会阻止其他进程获得任何锁。只能一个读或者一个写。*/
	public static void main(String[] args) {
		//TODO Auto-generated method stub
		/*new WriteThread("d:/test/test_1.txt", "d:/test/test.txt", "write_thread-1").start();
		new WriteThread("d:/test/logs", "d:/test/test.txt", "write_thread-2").start();  
		
		new ReadThread("d:/test/test.txt", "read_thread-1").start();
		new ReadThread("d:/test/test.txt", "read_thread-2").start();*/
		
		new ReadThread("d:/test/test.txt", "read_thread-1").start();
        new ReadThread("d:/test/test.txt", "read_thread-2").start();

        new NormalWriteThread("d:/test/test_copy1.txt", "d:/test/test.txt", "write_thread-3").start();
        new NormalWriteThread("d:/test/test_copy2.txt","d:/test/test.txt",  "write_thread-4").start();
        new NormalReadThread("d:/test/test.txt", "read_thread-3").start();
        new NormalReadThread("d:/test/test.txt", "read_thread-4").start();
	}

}

